package com.zcf.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zcf.common.json.Body;
import com.zcf.mapper.TtMapper;
import com.zcf.pojo.Taocanp;
import com.zcf.pojo.Tt;
import com.zcf.utils.Hutool;
import com.zcf.utils.UploadImgUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author loupeng
 * @since 2018-12-29
 */
@Controller
@RequestMapping("/tt")
@ResponseBody
@CrossOrigin
public class TtController {

	@Autowired
	TtMapper TtMapper;

//	/**
//	 * 添加图片
//	 *
//	 * @throws Exception
//	 */
//	@PostMapping("insert")
//	public Body insert(Tt Tt, MultipartFile[] file) throws Exception {
//		Tt.setId(Hutool.getId());
//		String files = UploadImgUtils.uploadFiles(file);
//		if (files != null && !"".equals(files)) {
//			Tt.setImg(files);
//		}
//		return Body.newInstance("添加失败");
//	}

	/**
	 * 查询大类
	 */
	@PostMapping("query")
	public Body query() {
		EntityWrapper<Tt> we = new EntityWrapper<>();
		List<Tt> list = TtMapper.selectList(we);
		if (list.size() > 0) {
			return Body.newInstance(list);
		}
		return Body.newInstance("查无数据");
	}

	/**
	 * 修改大类
	 */
	@PostMapping("updateTt")
	public Body updateTt(Tt Tt, MultipartFile[] file) throws Exception {
		Tt tt2 = new Tt();
		EntityWrapper<Tt> ew = new EntityWrapper<>();
		ew.eq("id", Tt.getId());
		tt2 = tt2.selectOne(ew);
		String files = UploadImgUtils.uploadFiles(file);
		if (files != null && !"".equals(files)) {
			tt2.setImg(files);
		}
		Boolean flag = tt2.updateById();
		if (flag) {
			return Body.newInstance(tt2);
		}
		return Body.BODY_500;
	}


	/**
	 * 根据id查询大类详情
	 */
	@PostMapping("queryOne")
	public Body queryOne() {
		List<Tt> tt = TtMapper.TtList();
		if (tt != null) {
			return Body.newInstance(tt.get(0));
		}
		return Body.BODY_500;
	}
}
